package com.cskaoyan._03otherstream._01data;

import java.io.*;

/**
 * @description:
 * @author: 景天
 * @date: 2023/2/20 11:37
 **/
/*
使用数据流读写数据

注意:
    读取的顺序要跟写的顺序保持一致
 */
public class Demo2 {
    public static void main(String[] args) throws IOException {
        writeMethod();
        // 使用数据输入流读取数据
        readMethod();

    }

    private static void readMethod() throws IOException {
        // 创建数据输入流对象
        DataInputStream in = new DataInputStream(new FileInputStream("a.txt"));



        // readInt()
        int i = in.readInt();
        System.out.println(i);

        // readDouble()
        double v = in.readDouble();
        System.out.println(v);

        boolean b = in.readBoolean();
        System.out.println(b);
        // close
        in.close();
    }

    private static void writeMethod() throws IOException {
        // 创建数据输出流对象
        DataOutputStream out = new DataOutputStream(new FileOutputStream("a.txt"));

        // writeInt
        // 写入1000
        out.writeInt(1000);

        // writeDouble
        out.writeDouble(3.14);

        out.writeBoolean(true);

        // close
        out.close();
    }
}
